package com.irany.component;

import com.irany.App;
import com.irany.enums.TaskTypeEnum;
import com.irany.model.MyReportInfo;
import com.irany.util.SeleniumUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * @author shujian.ou
 * @since 2021/5/17 0017 19:01
 */
public class MyAllReportCollector extends AbstractCollector {

    public MyAllReportCollector(App app) {
        super(app);
    }

    public void record() {
        WebDriver driver = getDriver();
        List<WebElement> taskList = driver.findElements(By.cssSelector("#issuetable > thead > tr"));

        for (WebElement t : taskList) {
            WebElement jiraKeywordElement = t.findElement(By.cssSelector("td.issuekey > a"));
            MyReportInfo.builder()
                    .keyword(jiraKeywordElement.getText())
                    .url(jiraKeywordElement.getAttribute("href"))
                    .projectName(t.findElement(By.cssSelector("td.project > a")).getText())
                    .priority(t.findElement(By.cssSelector("img")).getAttribute("alt"))
                    .title(t.findElement(By.cssSelector("td.summary > p > a:nth-child(2)")).getText())
                    .agentName(t.findElement(By.cssSelector("td.assignee > span > .user-hover")).getText())
                    .reporterName(t.findElement(By.cssSelector("td.reporter > span > .user-hover")).getText())
                    .createTime(t.findElement(By.cssSelector("td.created > span > time")).getText())
                    .status(t.findElement(By.cssSelector("td.status > span")).getText())
                    .solutionResult(t.findElement(By.cssSelector("td.resolution")).getText())
                    .solutionEndTime(t.findElement(By.cssSelector("td.resolutiondate > span > time")).getText())
                    .realOnlineTime(t.findElement(By.cssSelector("td:nth-child(12) > span > time")).getText())
                    .build();

        }
    }

    @Override
    public void run() {
        SeleniumUtils.openNewWindowAndSwitch(getDriver(), "http://jira.zmops.cc/issues/?jql=assignee in (" + getApp().getUserInfo().getUsername() + ") ORDER BY status DESC");
        record();
    }

}
